package com.zcf.utils;

import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URL;

import org.apache.commons.io.IOUtils;

import com.alibaba.fastjson.JSONObject;

public class JavaLarLng {
	public String getLarLng(String address) throws Exception {

		String ak = "Ea8qqpoPTSKv925ii3yZ2QFKfFK683xR";

		String addressUrl = "http://api.map.baidu.com/geocoder/v2/?address=" + address + "&output=json&ak=" + ak
				+ "&callback=showLocation";

		URL url = new URL(addressUrl);

		InputStream inputStream = url.openStream();

		String string = IOUtils.toString(inputStream);

		// showLocation&&showLocation({"status":0,"result":{"location":{"lng":121.4423987575458,"lat":30.939981749234133},"precise":1,"confidence":80,"level":"道路"}})

		System.out.println(string);

		int len = string.length();

		String substring = string.substring(27, len - 1);

		// {"status":0,"result":{"location":{"lng":121.4423987575458,"lat":30.939981749234133},"precise":1,"confidence":80,"level":"道路"}}
		System.out.println(substring);

		JSONObject jsonObject = JSONObject.parseObject(substring);

		String status = jsonObject.getString("status");

		Double lng = 0.0;

		Double lat = 0.0;
		if (status.equals("0")) {


			lng = jsonObject.getJSONObject("result").getJSONObject("location").getDouble("lng");

			lat = jsonObject.getJSONObject("result").getJSONObject("location").getDouble("lat");
		}

		if (Double.isNaN(lng)) {
			System.out.println(0);
		}
		BigDecimal bd = new BigDecimal(lng);
		// 保留小数点后六位 并做四舍五入
		double v = bd.setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();

		System.out.println(v);
		System.out.println(lat);
		return lng + "";
	}

	public String getLarLngs(String address) throws Exception {

		String ak = "Ea8qqpoPTSKv925ii3yZ2QFKfFK683xR";

		String addressUrl = "http://api.map.baidu.com/geocoder/v2/?address=" + address + "&output=json&ak=" + ak
				+ "&callback=showLocation";

		URL url = new URL(addressUrl);

		InputStream inputStream = url.openStream();

		String string = IOUtils.toString(inputStream);

		// showLocation&&showLocation({"status":0,"result":{"location":{"lng":121.4423987575458,"lat":30.939981749234133},"precise":1,"confidence":80,"level":"道路"}})

		System.out.println(string);

		int len = string.length();

		String substring = string.substring(27, len - 1);

		// {"status":0,"result":{"location":{"lng":121.4423987575458,"lat":30.939981749234133},"precise":1,"confidence":80,"level":"道路"}}
		System.out.println(substring);

		JSONObject jsonObject = JSONObject.parseObject(substring);

		String status = jsonObject.getString("status");

		Double lng = 0.0;

		Double lat = 0.0;

		if (status.equals("0")) {

			lng = jsonObject.getJSONObject("result").getJSONObject("location").getDouble("lng");

			lat = jsonObject.getJSONObject("result").getJSONObject("location").getDouble("lat");
		}

		if (Double.isNaN(lng)) {
			System.out.println(0);
		}
		BigDecimal bd = new BigDecimal(lat);
		// 保留小数点后六位 并做四舍五入
		double v = bd.setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();

		System.out.println(v);
		System.out.println(lat);
		return lat + "";
	}

	public static void main(String[] args) throws Exception {
		JavaLarLng javaLarLng = new JavaLarLng();
		String sum = javaLarLng.getLarLng("徐州铜山万达广场1、2、3、5、8号楼5-2402");
		String num = javaLarLng.getLarLngs("徐州铜山万达广场1、2、3、5、8号楼5-2402");
		System.err.println(sum);
		System.err.println(num);
	}
}
